﻿using System.Runtime.InteropServices;
using System.Text;

namespace CommonFramework
{
    public class IniFileAccess
    {
        private string filePath;

        public IniFileAccess()
        {
        }

        public IniFileAccess(string path)
        {
            this.filePath = path;
        }

        public void Write(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, this.filePath);
        }

        public string Read(string section, string key)
        {
            StringBuilder stringBuffer = new StringBuilder(255);
            GetPrivateProfileString(section, key, string.Empty, stringBuffer, 255, this.filePath);

            return stringBuffer.ToString();
        }

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(
                string section, 
                string key, 
                string val, 
                string filePath);

        [DllImport("Kernel32")]
        private static extern int GetPrivateProfileString(
                string section,
                string key,
                string def,
                StringBuilder retVal,
                int size,
                string filePath);
    }
}
